import random
from typing import Dict, List, Optional
from .age_group import AgeGroup

class EducationUtils:
    def __init__(self):
        # 年龄分级系统提示词
        self.age_prompts = {
            AgeGroup.LOWER_PRIMARY: """你是一个专门帮助1-2年级小朋友的助手。请注意：
- 使用非常简单的语言
- 多用图形和表情符号
- 每次回答控制在100字以内
- 多用夸奖和鼓励
- 引导基础认知和生活常识""",
            
            AgeGroup.MIDDLE_PRIMARY: """你是一个专门帮助3-4年级小朋友的助手。请注意：
- 使用生动有趣的例子
- 培养基础思维能力
- 鼓励动手实践
- 引导正确的学习方法
- 关注科学和自然知识""",
            
            AgeGroup.UPPER_PRIMARY: """你是一个专门帮助5-6年级小朋友的助手。请注意：
- 培养独立思考能力
- 引导知识关联和应用
- 鼓励提出问题
- 培养学习兴趣
- 关注综合素质发展"""
        }
        
        # 教育资源库
        self.educational_resources = {
            "数学": {
                AgeGroup.LOWER_PRIMARY: ["基础数数", "简单加减", "图形认知"],
                AgeGroup.MIDDLE_PRIMARY: ["乘除法", "分数初步", "简单应用题"],
                AgeGroup.UPPER_PRIMARY: ["小数运算", "比例问题", "几何思维"]
            },
            "语文": {
                AgeGroup.LOWER_PRIMARY: ["拼音学习", "基础识字", "简单造句"],
                AgeGroup.MIDDLE_PRIMARY: ["阅读理解", "作文基础", "成语故事"],
                AgeGroup.UPPER_PRIMARY: ["文章写作", "古诗文", "语言表达"]
            },
            "科学": {
                AgeGroup.LOWER_PRIMARY: ["生活常识", "植物观察", "天气认知"],
                AgeGroup.MIDDLE_PRIMARY: ["简单实验", "动物世界", "环境保护"],
                AgeGroup.UPPER_PRIMARY: ["科学探究", "生态系统", "科技发展"]
            }
        }
        
        # 互动游戏元素
        self.educational_games = {
            AgeGroup.LOWER_PRIMARY: [
                {
                    "name": "数字接龙",
                    "description": "我说一个数字，你接一个更大的数字",
                    "rules": "每个数字要比前一个大，但不能超过10"
                },
                {
                    "name": "找不同",
                    "description": "观察两幅图的区别",
                    "rules": "仔细看，说出不同的地方"
                }
            ],
            AgeGroup.MIDDLE_PRIMARY: [
                {
                    "name": "词语接龙",
                    "description": "用上一个词语的最后一个字开始",
                    "rules": "必须是正确的词语，不能重复"
                },
                {
                    "name": "脑筋急转弯",
                    "description": "解决有趣的智力问题",
                    "rules": "认真思考，可以请求提示"
                }
            ],
            AgeGroup.UPPER_PRIMARY: [
                {
                    "name": "知识问答",
                    "description": "回答综合性的知识问题",
                    "rules": "可以查阅资料，解释思考过程"
                },
                {
                    "name": "创意写作",
                    "description": "根据提示完成故事",
                    "rules": "发挥想象力，注意故事结构"
                }
            ]
        }

    def get_age_prompt(self, age_group: AgeGroup) -> str:
        """获取年龄分级的系统提示词"""
        return self.age_prompts.get(age_group, self.age_prompts[AgeGroup.MIDDLE_PRIMARY])

    def get_resources(self, subject: str, age_group: AgeGroup) -> List[str]:
        """获取特定学科和年龄段的教育资源"""
        return self.educational_resources.get(subject, {}).get(age_group, [])

    def get_random_game(self, age_group: AgeGroup) -> Dict:
        """获取随机的教育游戏"""
        games = self.educational_games.get(age_group, [])
        return random.choice(games) if games else None

    def format_response(self, content: str, age_group: AgeGroup) -> str:
        """根据年龄段格式化回答"""
        if age_group == AgeGroup.LOWER_PRIMARY:
            # 添加更多表情和简单语言
            content = content.replace("。", "。😊")
            content = content.replace("注意", "👀 注意")
            return content[:200]  # 限制长度
            
        elif age_group == AgeGroup.MIDDLE_PRIMARY:
            # 添加例子和互动元素
            content = content.replace("例如", "🌟 例如")
            content = content.replace("提示", "💡 提示")
            return content
            
        else:  # UPPER_PRIMARY
            # 保持原有格式，添加思考提示
            if len(content) > 300:
                content += "\n\n💭 思考一下：\n- 这些知识如何应用？\n- 还有什么问题想问？"
            return content

    def suggest_activity(self, subject: str, age_group: AgeGroup) -> str:
        """推荐教育活动"""
        resources = self.get_resources(subject, age_group)
        game = self.get_random_game(age_group)
        
        suggestion = f"让我们做些有趣的事情吧！\n\n"
        
        if resources:
            suggestion += f"📚 可以学习：\n"
            for resource in resources:
                suggestion += f"- {resource}\n"
                
        if game:
            suggestion += f"\n🎮 来玩个游戏：\n"
            suggestion += f"- {game['name']}\n"
            suggestion += f"- {game['description']}\n"
            suggestion += f"- 规则：{game['rules']}\n"
            
        return suggestion 